package com.jzo2o.health.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzo2o.api.orders.OrdersApi;
import com.jzo2o.api.trade.enums.PayChannelEnum;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.DateUtils;
import com.jzo2o.health.constant.RedisConstants;
import com.jzo2o.health.mapper.MemberMapper;
import com.jzo2o.health.mapper.OrdersMapper;
import com.jzo2o.health.mapper.SetmealMapper;
import com.jzo2o.health.model.domain.Member;
import com.jzo2o.health.model.domain.Orders;
import com.jzo2o.health.model.domain.Setmeal;
import com.jzo2o.health.model.dto.request.PlaceOrderReqDTO;
import com.jzo2o.health.model.dto.response.OrdersPayResDTO;
import com.jzo2o.health.model.dto.response.PlaceOrderResDTO;
import com.jzo2o.health.service.IUserOrderService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @Author: beiqian
 * @Date: 2024-07-29 15:07
 * @Description:
 */
@Service
public class IUserOrderServiceImpl implements IUserOrderService {

    @Resource
    private IUserOrderServiceImpl owner;
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private OrdersApi ordersApi;

    @Resource
    private MemberMapper memberMapper;
    @Resource
    private OrdersMapper ordersMapper;
    @Override
    public PlaceOrderResDTO place(PlaceOrderReqDTO placeOrderReqDTO) {

        //生成订单号
        Long orderId = generateOrderId();
        //创建订单对象
        Orders orders = new Orders();
        //设置订单号
        orders.setId(orderId);
        //设置体检人电话
        orders.setCheckupPersonPhone(placeOrderReqDTO.getCheckupPersonPhone());
        //设置套餐id
        orders.setSetmealId(placeOrderReqDTO.getSetmealId());
        //设置体检人性别
        orders.setCheckupPersonSex(placeOrderReqDTO.getCheckupPersonSex());
        //设置预约日期
        orders.setReservationDate(placeOrderReqDTO.getReservationDate());
        //设置体检人姓名
        orders.setCheckupPersonName(placeOrderReqDTO.getCheckupPersonName());
        //设置体检人身份证号
        orders.setCheckupPersonIdcard(placeOrderReqDTO.getCheckupPersonIdcard());
        //获取套餐
        Setmeal setmeal = setmealMapper.selectById(orders.getSetmealId());
        //设置套餐名称
        orders.setSetmealName(setmeal.getName());
        //设置套餐适用性别
        orders.setSetmealSex(setmeal.getSex());
        //设置套餐适用年龄
        orders.setSetmealAge(setmeal.getAge());
        //设置套餐图片
        orders.setSetmealImg(setmeal.getImg());
        //设置套餐说明
        orders.setSetmealRemark(setmeal.getRemark());
        //设置套餐价格
        orders.setSetmealPrice(new BigDecimal(setmeal.getPrice()));
        //设置用户id
        Long userid = UserContext.currentUserId();
        Member member = memberMapper.selectById(userid);
        //设置用户相关的信息
        orders.setMemberId(userid);
        orders.setMemberPhone(member.getPhone());

        long sortBy = DateUtils.toEpochMilli(LocalDateTimeUtil.now()) + orders.getId() % 100000;
        orders.setSortBy(sortBy);
        owner.add(orders);

        //返回数据
        PlaceOrderResDTO orderResDTO = PlaceOrderResDTO.builder().id(orderId).build();
        return orderResDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(Orders orders) {
        int insert = ordersMapper.insert(orders);
        if(insert == 0){
            throw new CommonException("插入失败");
        }
    }

    @Override
    public OrdersPayResDTO pay(Long id, PayChannelEnum tradingChannel) {
        Orders orders = ordersMapper.selectById(id);

        return null;
    }

    private Long generateOrderId() {
        Long increment = redisTemplate.opsForValue().increment(RedisConstants.ORDER_GENERATE_ID);
        long orderId = DateUtils.getFormatDate(LocalDateTime.now(), "yyMMdd") * 10000000000000L + increment;
        return orderId;
    }
}
